package czy.demo.controller;


import czy.demo.pojo.Person;
import czy.demo.repository.PersonRepository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/repository")
public class ElasticsearchRepositoryController {

    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchRepositoryController.class);

    @Autowired
    private PersonRepository personRepository;

    /**
     * 查询语句查询接口，可以在多个字段中进行查询，并且返回所有包含查询内容的实体
     * es的模糊查询就是通过这个来进行的，这个接口除了会返回content字段之外，还会返回很多额外的分页信息
     * @param query
     * @return
     */
    @GetMapping("/search/{query}")
    public Page search(@PathVariable String query){
        QueryBuilder builder = QueryBuilders.queryStringQuery(query);
        NativeSearchQuery searchQuery = new NativeSearchQuery(builder);
        return this.personRepository.search(searchQuery);
    }

    @GetMapping("/search/all")
    public Page queryAll(){
        QueryBuilder builder = QueryBuilders.matchAllQuery();
        NativeSearchQuery searchQuery = new NativeSearchQuery(builder);
        return this.personRepository.search(searchQuery);
    }

    /**
     * multiMatchQuery可以进行多字段内容匹配
     */
    @PostMapping("/search/multi")
    public Page queryMulti(@RequestBody String content){
        QueryBuilder builder = QueryBuilders.multiMatchQuery(content,"name","description");
        NativeSearchQuery searchQuery = new NativeSearchQuery(builder);
        return this.personRepository.search(searchQuery);
    }

    /* 高亮可以查询，但是无法展示高亮结果 */
    @GetMapping("/search/highlight")
    public Page queryHighLight(){
        QueryBuilder builder = QueryBuilders.matchAllQuery();
        HighlightBuilder highlightBuilder = new HighlightBuilder().preTags("<hehe>").postTags("</hehe>");
        HighlightBuilder.Field field = new HighlightBuilder.Field("name");
        HighlightBuilder.Field desc = new HighlightBuilder.Field("description");
        NativeSearchQuery searchQuery = new NativeSearchQuery(builder,null,null,highlightBuilder,new HighlightBuilder.Field[]{field,desc});
        return this.personRepository.search(searchQuery);
    }

    /* matchQuery指定了字段匹配，并不是必须相等，包含就可以 */
    @GetMapping("/name/{name}")
    public Page queryByName(@PathVariable String name){
        QueryBuilder builder = QueryBuilders.matchQuery("name",name);
        NativeSearchQuery searchQuery = new NativeSearchQuery(builder);
        return this.personRepository.search(searchQuery);
    }

    @GetMapping("/person/count")
    public long count(){
        return this.personRepository.count();
    }

    @GetMapping("/person/{id}")
    public Person get(@PathVariable String id){
        return this.personRepository.findById(id).get();
    }

    @GetMapping("/person/exist/{id}")
    public boolean exist(@PathVariable String id){
        return this.personRepository.existsById(id);
    }

    @PostMapping("/person/create")
    public Person create(@RequestBody Person person){
        return this.personRepository.save(person);
    }

    @PostMapping("/person/create/many")
    public Iterable<Person> create(@RequestBody List<Person> persons){
        return this.personRepository.saveAll(persons);
    }

    @DeleteMapping("/person/delete/{id}")
    public void delete(@PathVariable String id){
        this.personRepository.deleteById(id);
    }

    @DeleteMapping("/person/delete/all")
    public void deleteAll(){
        this.personRepository.deleteAll();
    }


}
